﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Xml;
using System.Windows.Forms;

using TLL.UTL;
using TLL.Debug;

namespace TLL.Finance.Currencys
{
    public class CurrencyRepository : IDisposable
    {
        public List<Currency> Currencys = new List<Currency>();

        public bool Access = true;
        public Error Error = null;
        public Warning Warning = null;
        public CurrencyRepository(CashRateType type, string CashRateUrl, string debugURL, NotifyIcon notify)
        {
            var url = type == CashRateType.CashRate ? CashRateUrl + "5" : CashRateUrl + "11";
            try
            {
                using (XmlReader reader = XmlReader.Create(url))
                    while (reader.Read())
                    {
                        if (reader.NodeType != XmlNodeType.Element) continue;
                        if (reader.GetAttribute("ccy") != null)
                        {
                            var currencyName = reader.GetAttribute("ccy") == "RUR" ? "RUB" : reader.GetAttribute("ccy");
                            var currencyType = Utilities.GetEnum<CurrencyType>(currencyName);
                            this.Currencys.Add(new Currency(currencyType, double.Parse(reader.GetAttribute("buy").Replace('.', ',')), double.Parse(reader.GetAttribute("sale").Replace('.', ','))));
                        }
                    }
                DebugLogRepository.Add(InformationType.Information, string.Format(@"CurrencyRepository, CurrencyRepository(CashRateType type[name = {0}]). Success operation", type.ToString()), debugURL);
            }
            catch 
            {
                this.Error = new Error(ErrorType.Get);
                this.Warning = new Warning(WarningType.Internet, notify, delegate(object sender, EventArgs e)
                {
                    this.Error.Show(DisplayDebugType.MessageBox);
                });
                DebugLogRepository.Add(InformationType.Error, string.Format(@"CurrencyRepository, CurrencyRepository(CashRateType type[name = {0}])
                                           Error title: {1}
                                           Error message: {2}", type.ToString(), this.Warning.Title, this.Warning.Message), debugURL);
                this.Access = false; 
            }
        }
        public List<Currency> Get(CurrencyType Type = CurrencyType.Default)
        {
            var res = new List<Currency>();
            switch (Type)
            {
                case CurrencyType.RUB: res.Add(this.Currencys.FirstOrDefault(x => x.Type == CurrencyType.RUB)); break;
                case CurrencyType.USD: res.Add(this.Currencys.FirstOrDefault(x => x.Type == CurrencyType.USD)); break;
                default: return this.Currencys; break;
            }
            return res;
        }
        public void Dispose()
        {
            if (this.Warning != null)
                this.Warning.Show(DisplayDebugType.Notify);

            this.Error = null;
            this.Currencys = null;
        }
    }
}
